var express = require('express');
var router = express.Router();

const sha1 = require("sha1");
const {getTicket} = require('../utils/accsToken');


async function getSignature(){
  const ticket = await getTicket();
  const noncestr = Math.random().toString().split(".")[1];
  const timestamp = Date.now();
  const arr = [
    `noncestr=${noncestr}`,
    `jsapi_ticket=${ticket}`,
    `timestamp=${timestamp}`,
    `url=http://lang11.liuxiaoyi.com.cn/main`
  ]
  const str = arr.sort().join('&');
  const signature = sha1(str);
  return {signature, timestamp, noncestr};
}

router.get('/', async function(req, res, next) {
  const signatureObj = await getSignature();
  res.render('index',{...signatureObj})
});

module.exports = router;
